package com.iss.apartmentmanage.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.iss.apartmentmanage.entity.Permission;
import com.iss.apartmentmanage.entity.User;

import com.iss.apartmentmanage.model.UserWithRole;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {
    @Select("select user.id,u_name,role.id,role.role_name from user " +
            "inner join user_role on `user`.id=user_role.role_id " +
            "INNER JOIN role ON role.id=user_role.role_id " +
            "where `user`.u_name=#{uName} " +
            "and `user`.u_pwd=#{uPwd} ")
    UserWithRole login(User user);
    //通过用户姓名查询用户信息
    @Select("select * from user where u_name=#{uName}")
    User selectAllUserByUserName(String uName);
    //通过用户名查询用户权限
    @Select("SELECT * FROM permission WHERE id in" +
            "(" +
            "SELECT perm_id FROM role_permission WHERE role_id in(" +
            "SELECT role_id FROM user_role WHERE user_id=#{id}" +
            ")" +
            ")")
    List<Permission> findPermissionByUsername(Integer uId);

    @Select("select user.id from user where u_name=#{uName}")
    Integer getUserIdByUserName(String uName);
}
